package com.aco.fatureapp.auxiliary;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.aco.fatureapp.R;

public class MenuAdapter extends ArrayAdapter<MenuItem> {

	List<MenuItem> drawerMenuItem;

	Context context;

	int layoutResId;

	public MenuAdapter(final Context context, final int layoutResourceID,
			final List<MenuItem> listItems) {
		super(context, layoutResourceID, listItems);
		this.context = context;
		this.drawerMenuItem = listItems;
		this.layoutResId = layoutResourceID;

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.widget.ArrayAdapter#getView(int, android.view.View,
	 * android.view.ViewGroup)
	 */
	@Override
	public View getView(final int position, final View convertView,
			final ViewGroup parent) {
		DrawerItemHolder drawerHolder;

		View view = convertView;

		if (view == null) {
			final LayoutInflater inflater = ((Activity) context)
					.getLayoutInflater();
			drawerHolder = new DrawerItemHolder();

			view = inflater.inflate(layoutResId, parent, false);

			drawerHolder.ItemName = (TextView) view
					.findViewById(R.id.menu_title);

			drawerHolder.icon = (ImageView) view.findViewById(R.id.menu_icon);

			view.setTag(drawerHolder);
		} else {
			drawerHolder = (DrawerItemHolder) view.getTag();
		}

		final MenuItem dItem = this.drawerMenuItem.get(position);

		drawerHolder.icon.setImageDrawable(view.getResources().getDrawable(
				dItem.getIconId()));

		drawerHolder.ItemName.setText(dItem.getTitle());

		return view;
	}

	private static class DrawerItemHolder {
		TextView ItemName;

		ImageView icon;
	}
}